package com.jianying;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

@Component("authInitInterceptor")
public class AuthInitInterceptor implements HandlerInterceptor{
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// do nothing
		
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		request.setAttribute("currentTenant", null);
		request.setAttribute("currentUser", null);
	}
	
//	private void authFail(HttpServletResponse response) throws IOException{
//		response.setHeader("content-type", "application/json");
//		response.setStatus(401);
//		response.getWriter().write("{\"error\": \"auth error\"}");
//	}
//	
//	private boolean ignoreAuth(HttpServletRequest request) {
//		return request.getRequestURI().equals("/") && request.getMethod().toLowerCase().equals("get")
//				|| request.getRequestURI().contains("/tenants/by-unionid/") && request.getMethod().toLowerCase().equals("get")
//				|| request.getRequestURI().contains("/tenants/by-phone/") && request.getMethod().toLowerCase().equals("get")
//				|| request.getRequestURI().endsWith("/tenants") && request.getMethod().toLowerCase().equals("post")
//				|| request.getRequestURI().endsWith("/tenants/login") && request.getMethod().toLowerCase().equals("post")
//				|| request.getRequestURI().contains("/tenants/exists") && request.getMethod().toLowerCase().equals("get")
//				|| request.getRequestURI().contains("/barter-tenant/") && request.getMethod().toLowerCase().equals("get")
//				|| request.getRequestURI().contains("/tenants/barter-we-open-api-tenant/") && request.getMethod().toLowerCase().equals("get")
//				|| request.getRequestURI().contains("/tenants/barter-we-app-session/") && request.getMethod().toLowerCase().equals("get")
//				|| request.getRequestURI().contains("/tenants/barter-we-gzh-session/") && request.getMethod().toLowerCase().equals("get")
//				|| request.getRequestURI().contains("/decode-encrypted-data") && request.getMethod().toLowerCase().equals("post")
//				|| request.getRequestURI().contains("/send-verification-code") && request.getMethod().toLowerCase().equals("post")
//				|| request.getRequestURI().contains("/error")
//				|| request.getRequestURI().contains("/auths/login") && request.getMethod().toLowerCase().equals("post");
//		
//	}
//	
}
